# Script installs packages necessary to replicate analyses 
# Last Updated: January 15, 2024

# For each package, the script first checks if the correct version of the package 
# is installed; if not, the script installs the package from the CRAN Archive. 
# Otherwise, it loads the required packages.

# tidyverse (Version 1.3.1)
if((!"tidyverse" %in% installed.packages()) | # If package is not installed 
   packageVersion("tidyverse") != "1.3.1"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/tidyverse/tidyverse_1.3.1.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("tidyverse")
} else { # Otherwise, load package
  library("tidyverse")
}

# lubridate (Version 1.8.0)
if((!"lubridate" %in% installed.packages()) | # If package is not installed 
   packageVersion("lubridate") != "1.8.0"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/lubridate/lubridate_1.8.0.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("lubridate")
} else { # Otherwise, load package
  library("lubridate")
}

# gtools (Version 3.9.2.2)
if((!"gtools" %in% installed.packages()) | # If package is not installed 
   packageVersion("gtools") != "3.9.2.2"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/gtools/gtools_3.9.2.2.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("gtools")
} else { # Otherwise, load package
  library("gtools")
}

# PanelMatch (Version 1.0.0)
if((!"PanelMatch" %in% installed.packages()) | # If package is not installed 
   packageVersion("PanelMatch") != "1.0.0"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/PanelMatch/PanelMatch_1.0.0.tar.gz" 
  install.packages(package_url, repos=NULL, type="source")
  library("PanelMatch")
} else { # Otherwise, load package
  library("PanelMatch")
}

# glue (Version 1.6.1)
if((!"glue" %in% installed.packages()) | # If package is not installed 
   packageVersion("glue") != "1.6.1"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/glue/glue_1.6.1.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("glue")
} else { # Otherwise, load package
  library("glue")
}

# stargazer (Version 5.2.1)
if((!"stargazer" %in% installed.packages()) | # If package is not installed 
   packageVersion("stargazer") != "5.2.1"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/stargazer/stargazer_5.2.1.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("stargazer")
} else { # Otherwise, load package
  library("stargazer")
}

# xtable (Version 1.8.3)
if((!"xtable" %in% installed.packages()) | # If package is not installed 
   packageVersion("xtable") != "1.8.3"){ # Or the installed version is not the correct version
  # Install and load correct version of package
  package_url <- "https://cran.r-project.org/src/contrib/Archive/xtable/xtable_1.8-3.tar.gz"
  install.packages(package_url, repos=NULL, type="source")
  library("xtable")
} else { # Otherwise, load package
  library("xtable")
}


# Confirm correct packages are installed 
packageVersion("tidyverse") == "1.3.1"
packageVersion("lubridate") == "1.8.0"
packageVersion("gtools") == "3.9.2.2"
packageVersion("PanelMatch") == "1.0.0"
packageVersion("glue") == "1.6.1"
packageVersion("stargazer") == "5.2.1"
packageVersion("xtable") == "1.8.3"
